package com.nuanqian.springbootdubboclient.utils;

import org.apache.activemq.ScheduledMessage;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.jms.core.JmsMessagingTemplate;

import javax.jms.*;
import java.io.Serializable;
import java.util.Queue;

/**
 * @author 李蒙
 * @date 2020/7/3 14:58
 */
public class ActiveMqUtils {

    public static ActiveMqUtils mqUtils= new ActiveMqUtils();

    private ActiveMqUtils(){

    }

    /**
     * 发送消息
     * @param template jms消息模板对象
     * @param destination destination是发送到的队列
     * @param message message是待发送的消息
     */
    public <T extends Serializable> void send(JmsMessagingTemplate template,Destination destination, T message){
        template.convertAndSend(destination, message);
    }

    /**
     * 延时发送
     * @param destination 发送的队列
     * @param data 发送的消息
     * @param time 延迟时间
     */
    public <T extends Serializable> void delaySend(JmsMessagingTemplate template,Destination destination, T data, Long time){
        Connection connection = null;
        Session session = null;
        MessageProducer producer = null;
        // 获取连接工厂
        ConnectionFactory connectionFactory = template.getConnectionFactory();
        try {
            // 获取连接
            connection = connectionFactory.createConnection();
            connection.start();
            // 获取session，true开启事务，false关闭事务  第二个参数：应答模式，建议使用自动应答
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 通过session创建一个消息队列
            producer = session.createProducer(destination);
            producer.setDeliveryMode(JmsProperties.DeliveryMode.PERSISTENT.getValue());
            ObjectMessage message = session.createObjectMessage(data);
            //设置延迟时间
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);

/*            延迟30秒，投递10次，间隔10秒:
            long delay = 30 * 1000;
            long period = 10 * 1000;
            int repeat = 9;
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);
            message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);
            */

//            CRON表达式的优先级高于另外三个参数，如果在设置了CRON的同时，也有repeat和period参数，
//            则会在每次CRON执行的时候，重复投递repeat次，每次间隔为period。就是说设置是叠加的效果。例如每小时都会发生消息被投递10次，延迟1秒开始，每次间隔1秒:
//            message.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, "0 * * * *");

            // 发送消息
            producer.send(message);
            session.commit();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            close(connection, session, producer);
        }
    }

    private void close(Connection connection, Session session, MessageProducer producer) {
        try {
            if (producer != null){
                producer.close();
            }
            if (session != null){
                session.close();
            }
            if (connection != null){
                connection.close();
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}
